package com.slx.rectangle_area;

public class Solution {

    /**
     * <pre>
     * Find the total area covered by two rectilinear rectangles in a 2D plane.
     * Each rectangle is defined by its bottom left corner and top right
     * corner as shown in the figure.
     *
     * Assume that the total area is never beyond the maximum possible value of int.
     *
     * *****************************(C,D):(3,4)
     * *                           *
     * *                           *
     * *           *************************************(G,H):(9,2)
     * *           *               *                   *
     * *           *               *                   *
     * *           *               *                   *
     * *           *               *                   *
     * *           *(0,0)          *                   *
     * *****************************                   *
     * (A,B):(-3,0)*                                   *
     *             *                                   *
     *             * (E,F):                 *
     *             *************************************
     *
     *   ***************************        ***************************
     *   *            D            *        *            H            *
     *   *                         *        *                         *
     *   *A                       C*        *E                       G*
     *   *                         *        *                         *
     *   *                         *        *                         *
     *   *            B            *        *            F            *
     *   ***************************        ***************************
     *
     *
     * @param A
     * @param B
     * @param C
     * @param D
     * @param E
     * @param F
     * @param G
     * @param H
     * @return
     */
    public int computeArea(int A, int B, int C, int D, int E, int F, int G, int H) {
        long area = (long) (C - A) * (D - B) + (long) (G - E) * (H - F);
        long width = Math.max((long) Math.min(C, G) - (long) Math.max(A, E), 0);
        long height = Math.max((long) Math.min(D, H) - (long) Math.max(B, F), 0);

        System.out.println(width + ":" + height + "=" + width * height);

        return (int) (area - width * height);
    }


}
